package com.myteam.mystory.DAO;

import java.util.ArrayList;
import java.util.List;

import com.myteam.mystory.entity.CategoryEntity;
import com.myteam.mystory.utils.LogUtils;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class CategoriesDAO {
	public static final String TAG = "CategoriesDAO";
	private final SQLiteDatabase db;
	
	public CategoriesDAO(SQLiteDatabase db) {
		super();
		this.db = db;
	}
	
	public long insert_categories(CategoryEntity categoryEntity){
		ContentValues value = new ContentValues();
		value.put(CategoriesScheme.CATID, categoryEntity.getCatId());
		value.put(CategoriesScheme.CATNAME, categoryEntity.getCatName());
		value.put(CategoriesScheme.DESCRIPTION, categoryEntity.getDescription());
		if(db.insert(CategoriesScheme.TABLE_CATEGORIES_NAME, null, value) == -1){
			LogUtils.e("Bug insert categgories");
		} else {
			LogUtils.e("successfully insert categories");
		}
		return 0;
	}
	
	public ArrayList<CategoryEntity> getAllCategories() {
		ArrayList<CategoryEntity> list_cate = new ArrayList<CategoryEntity>();
		String query = "select * from " + CategoriesScheme.TABLE_CATEGORIES_NAME;
		Cursor cursor = db.rawQuery(query, null);
		if(cursor.moveToFirst()){
			do {
				CategoryEntity categoryEntity = new CategoryEntity();
				categoryEntity.setCatId(cursor.getInt(0));
				categoryEntity.setCatName(cursor.getString(1));
				categoryEntity.setDescription(cursor.getString(2));
				list_cate.add(categoryEntity);
			} while (cursor.moveToNext());
		}
		return list_cate;
	}
	
	public List<String> getCateName(){
		String query = "select * from " + CategoriesScheme.TABLE_CATEGORIES_NAME;
		Cursor cursor = db.rawQuery(query, null);
		List<String> arr_cateName = new ArrayList<String>();
		if(cursor.moveToFirst()){
			do {
				String cateName = cursor.getString(1);
				arr_cateName.add(cateName);
			} while(cursor.moveToNext());
		}
		return arr_cateName;
	}
	
	public long deleteCategoryTable(){
		int result = 0;
		try {
			result = db.delete(CategoriesScheme.TABLE_CATEGORIES_NAME, null, null);
			LogUtils.e("delete table category successfully");
		} catch (Exception e) {
			LogUtils.e("delete table category fail");
		}
		return result;
	}
}
